//
//  YFTabBarViewController.m
//  Novel
//
//  Created by 孙云飞 on 2017/10/30.
//  Copyright © 2017年 th. All rights reserved.
//

#import "YFTabBarViewController.h"
#import "BookshelfVC.h"
#import "RankingVC.h"
#import "SearchVC.h"
#import "RootNavViewController.h"
@interface YFTabBarViewController ()

@end

@implementation YFTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    RootNavViewController *homeNav = [self loadVC:[[BookshelfVC alloc]init] andTitle:@"书架" andDefaultImage:@"读书灰" andSelectedImage:@"读书蓝色"];
    
    RootNavViewController *searchNav = [self loadVC:[[RankingVC alloc]init] andTitle:@"发现" andDefaultImage:@"发现灰" andSelectedImage:@"发现蓝色"];
    
    RootNavViewController *findNav = [self loadVC:[[SearchVC alloc]init] andTitle:@"搜索" andDefaultImage:@"搜索灰" andSelectedImage:@"搜索蓝色"];
    
    self.tabBar.tintColor = [UIColor colorWithRed:55.0 / 255 green:184.0 / 255 blue:245.0 / 255 alpha:1.0];
    self.viewControllers = @[homeNav,searchNav,findNav];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (RootNavViewController *)loadVC:(UIViewController *)vc
                         andTitle:(NSString *)title
                  andDefaultImage:(NSString *)dImage
                 andSelectedImage:(NSString *)sImage{
    RootNavViewController *rootNav = [[RootNavViewController alloc]initWithRootViewController:vc];
    vc.title = title;
    UIImage *image = [self scaleToSize:[UIImage imageNamed:dImage] size:CGSizeMake(30,30)];
    UIImage *selectImage = [self scaleToSize:[UIImage imageNamed:sImage] size:CGSizeMake(30,30)];
    vc.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return rootNav;
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(newsize);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}
@end
